package com.zw.pm.module.system.user;

import com.zw.pm.base.BaseController;
import com.zw.pm.common.JsonResult;
import com.zw.pm.common.ValidResult;
import com.zw.pm.gen.entity.Role;
import com.zw.pm.gen.entity.User;
import com.zw.pm.module.system.log.LogService;
import com.zw.pm.module.system.role.RoleModel;
import com.zw.pm.module.system.role.RoleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.zw.pm.constant.Constant.STATUS_NORMAL;

/**
 * @Description: 用户 控制层
 *
 * @Author: China.sgl
 * @Date: 2019/9/16 09:11
 */
@Controller
@RequestMapping("/sys/user")
public class UserController extends BaseController {

    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private LogService logService;

    /**
     * 系统帐户页面
     * @return 页面 URL
     */
    @GetMapping
    @RequiresPermissions({"system:user"})
    public String page() {
        return "system/user/page";
    }

    /**
     * 系统帐户数据
     * @param user 查询条件封装
     * @return 系统帐户 Json 数据
     */
    @ResponseBody
    @PostMapping("/list")
    @RequiresPermissions({"system:user"})
    public JsonResult userList(User user) {
        startPage();
        List<User> list = userService.findAll(user);
        return jsonResult(list);
    }

    /**
     * 修改帐户
     * @param user 帐户信息
     * @return json数数
     */
    @ResponseBody
    @PostMapping("/update")
    @RequiresPermissions({"system:user"})
    public JsonResult update(User user) {
        logService.saveLog((user.getStatus() == STATUS_NORMAL ? "解锁用户" : "锁定用户") ,user.getName());
        return jsonResult(userService.updateUser(user));
    }

    /**
     * 重置帐户密码
     * @param id 帐户 id
     * @return json数据
     */
    @ResponseBody
    @PostMapping("/reset")
    @RequiresPermissions({"system:user"})
    public JsonResult resetPassword(Integer id) {
        return jsonResult(userService.resetPassword(id));
    }

    /**
     * 查询所有角色
     * @return json数据
     */
    @GetMapping("/roles")
    @ResponseBody
    @RequiresPermissions({"system:user"})
    public List<RoleModel> roles() {
        return roleService.findAll(new Role());
    }

    /**
     * 检查帐号是否存在
     * @param account 帐号
     * @return json 数据
     */
    @ResponseBody
    @PostMapping("/checkUnique")
    @RequiresPermissions({"system:user"})
    public ValidResult checkUnique(String account) {
        User user = userService.findByAccount(account);
        return ValidResult.result(user);
    }

    /**
     * 创建帐户
     * @param user 帐户信息
     * @param roleIds 选中角色 id 数组
     * @return json 数据
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions({"system:user"})
    public JsonResult save(User user, Integer[] roleIds) {
        logService.saveLog("创建用户", user.getName());
        return jsonResult(userService.saveUser(user, roleIds));
    }

    /**
     * 修改帐户与角色
     * @param user 帐户信息
     * @param roleIds 选中角色 id 数组
     * @return json 数据
     */
    @ResponseBody
    @PostMapping("/edit")
    @RequiresPermissions({"system:user"})
    public JsonResult update(User user, Integer [] roleIds) {
        User u = userService.findUserById(user.getId());
        logService.saveLog("修改用户", u, user);
        return jsonResult(userService.updateUser(user, roleIds));
    }

    /**
     * 查询所有角色，标识帐户拥有角色
     * @param id 帐户 id
     * @return json 数据
     */
    @GetMapping("/roles/{id}")
    @ResponseBody
    @RequiresPermissions({"system:user"})
    public List<RoleModel> roles(@PathVariable Integer id) {
        return roleService.findRolesCheckedByUserId(id);
    }

    /**
     * 查询用户
     * @param id 用户id
     * @return 用户
     */
    @ResponseBody
    @PostMapping("/id")
    @RequiresPermissions({"system:user"})
    public User id(Integer id) {
        return userService.findUserById(id);
    }

}
